package com.atlin.project.controller;


import cn.hutool.core.io.FileUtil;
import com.atlin.project.common.BaseResponse;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.common.ResultUtils;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.fileService.BaseFileService;
import com.atlin.project.fileService.FileServiceManager;
import com.atlin.project.manager.RedisLimiterManager;
import com.atlin.project.model.domain.SysUser;
import com.atlin.project.model.dto.file.PreviewFileRequest;
import com.atlin.project.model.dto.file.UploadFileRequest;
import com.atlin.project.model.enums.FileUploadBizEnum;
import com.atlin.project.service.SysUserService;
import com.atlin.project.utils.file.LocalFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 文件接口
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private RedisLimiterManager redisLimiterManager;

    @Resource
    private FileServiceManager fileServiceManager;
    

    @PostMapping("/upload")
    public BaseResponse<String> uploadFile(@RequestPart("file") MultipartFile multipartFile,
                                              UploadFileRequest uploadFileRequest, HttpServletRequest request) {
        String biz = uploadFileRequest.getBiz();
        FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
        if (fileUploadBizEnum == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysUser loginUser = sysUserService.getLoginUser(request);

        BaseFileService fileService = fileServiceManager.getFileService();
        String doSave = fileService.doSave(multipartFile, loginUser, fileUploadBizEnum);
        return ResultUtils.success(doSave);
    }

    @PostMapping("/previewFile")
    public void previewFile(@RequestBody PreviewFileRequest previewFileRequest, HttpServletRequest request, HttpServletResponse response) {
        ThrowUtils.throwIf(previewFileRequest == null, ErrorCode.PARAMS_ERROR);
        String filePath = previewFileRequest.getFilePath();
        ThrowUtils.throwIf(StringUtils.isBlank(filePath), ErrorCode.PARAMS_ERROR);

        // TODO 权限校验
        SysUser loginUser = sysUserService.getLoginUser(request);

        BaseFileService fileService = fileServiceManager.getFileService();
        fileService.doRead(filePath, request, response);
    }

    @GetMapping("/previewImage")
    public void previewImage(String imagePath, HttpServletRequest request, HttpServletResponse response) {
        ThrowUtils.throwIf(StringUtils.isBlank(imagePath), ErrorCode.PARAMS_ERROR);

        // TODO 权限校验 文件类型校验 只支持图片类型文件
        SysUser loginUser = sysUserService.getLoginUser(request);
        
        String suffix = FileUtil.getSuffix(imagePath);
        final List<String> accSuffix = Arrays.asList("jpg", "png", "jpeg", "gif");
        ThrowUtils.throwIf(!accSuffix.contains(suffix), ErrorCode.NO_AUTH_ERROR);

        // 限流 
//        redisLimiterManager.doRateLimiter("previewImage_" + loginUser.getId());

        // 设置缓存控制头
        long TEN_HOUR = 60 * 60 * 10L;
        response.setHeader("Cache-Control", "public, max-age=" + TEN_HOUR);

        LocalFileUtil.previewFile(imagePath, response);
    }
}
